android 畫圖表的第三方工具
(一)BarChart
導入
implementation
XML中建立view
Activity中設定
val chart = bar_chart
定義左邊Y軸座標
chart.axisLeft.apply{
axisMinimum = 0f
axisMaximum = 100f
labelCount = 5
setDrawTopYLabelEntry(true)
setValueFormatter{
value, axis ->""+value.toInt()
}
}
定義右邊Y軸座標
chart.axisRight.apply {
setDrawTopYLabelEntry(true)
setDrawZeroLine(false)
setDrawGridLines(false)
setDrawLabels(false)
}
定義X軸標籤
val labels = arrayOf("","國文","數學","英文") [0]為原點座標
chart.xAxis.apply {
valueFormatter = IndexAxisValueFormatter(labels)
labelCount = 3
position = XAxis.XAxisPosition.BOTTOM
setDrawLabels(true)
setDrawGridLines(false)
定義圖表
chart.apply {
setDrawValueAboveBar(true)
description.isEnabled = false
isClickable = false
legend.isEnabled = false
setScaleEnabled(false)
animateY(1200, Easing.EasingOption.Linear)
}
設定資料
private fun getBarData(): ArrayList? {
val entries = ArrayList().apply {
add(BarEntry(1f, 60f))
add(BarEntry(2f, 80f))
add(BarEntry(3f, 70f))
val dataSet = BarDataSet(entries, "bar").apply {
valueFormatter = IValueFormatter { value, _, _, _ -> "" + value.toInt() }
isHighlightEnabled = false
setColors(intArrayOf(R.color.material_blue, R.color.material_green, R.color.material_yellow), this@barchart)
}
val bars = ArrayList<IBarDataSet>()
bars.add(dataSet)
return bars